package com.mail;

import org.junit.Test;

import java.io.*;
import java.text.SimpleDateFormat;

public class Report {

	public String frameWorkPath;
	public String folderPath;
	public String testCaseName;
    public String ActionName;

    private int Pass_number=0;
    private int Fai_number=0;
    private int Warning_number=0;
    
    private int  Tc_PassNumber=0;
    private int  Tc_FailNumber=0;
    private int  Tc_Warning_number=0;
    
    private String executFlag="Yes";
    //private String browserType="";
    
    public int  Total_PassNumber=0;
    public int  Total_FailNumber=0;
    public int  Total_Warning_number=0;
    
    public String startDate="";
    public String endDate="";
    //public String[] mail_to={"long.yu@china.zhaogang.com"};
    //public String[] mail_cc={"long.yu@china.zhaogang.com"};
    public String[] mail_to={"1405201500@qq.com","wuy@china.zhaogang.com"};
    public String[] mail_cc={"1405201500@qq.com","wuy@china.zhaogang.con"};
//    public String[] mail_to={"wuy@china.zhaogang.com","leon@zhaogang.com","ruiao.tang@china.zhaogang.com","swp@china.zhaogang.com","zyn@zhaogang.com","zhl@china.zhaogang.com","fangxinfei@china.zhaogang.com","long.yu@china.zhaogang.com"};
//    public String[] mail_cc={"zxk@zhaogang.com"};
    //public String BrowserType;
    

    String var_HTML = folderPath+ "\\report.html";
    StringBuffer initreportbuffer=new StringBuffer();
    StringBuffer inittcbuffer=new StringBuffer();
    StringBuffer actionBuf =  new StringBuffer();
    
    public void setMailTo(String[] mail_to){
    	 	this.mail_to = mail_to;
    		
    }
    public void setExecutFlag(String executFlag) {
		this.executFlag = executFlag;
	}
    
    public String getExecutFlag() {
		return executFlag;

    }
/*    
    public void setBrowserType(String browserType) {
		this.browserType = browserType;
	}
   
    public String getBrowserType() {
		return this.browserType;

    }
*/    
    public void setStartDate() {
    	SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");      
    	startDate=sDateFormat.format(new java.util.Date());  
	   
	}
    public String getStartDate() {
    	return this.startDate;
	}
    public String getEndDate() {
    	SimpleDateFormat   sDateFormat   =   new   SimpleDateFormat("yyyy-MM-dd hh:mm:ss");      
    	this.endDate=sDateFormat.format(new   java.util.Date()); 
    	return this.endDate;
	}
    
   
    
	public void initReport(String frameWorkPath,String folderPath) throws IOException{
		this.folderPath = folderPath;
		this.frameWorkPath=frameWorkPath ;
		//Add folderPath info to config.Bleum
		String configFilePath="";
		if (!new File(frameWorkPath).exists())
			new File(frameWorkPath).mkdirs();
		configFilePath=frameWorkPath+"\\config.ZhaoGang";
		Writer bw = new BufferedWriter(new FileWriter(configFilePath,true));
		bw.write(folderPath);
		bw.write("\r\n");
		bw.close();
		
		//Create HTML Header
		
		initreportbuffer.append("<title>");
		initreportbuffer.append("\n");
		initreportbuffer.append("Report");
		initreportbuffer.append("\n");
		initreportbuffer.append("</title>");
		initreportbuffer.append("\n");
		initreportbuffer.append("<body>");
		initreportbuffer.append("\n");
		initreportbuffer.append("<font face='Tahoma'size='2'>");
		initreportbuffer.append("\n");
		initreportbuffer.append("<h1>Automation Test Results - Bleum</h1>");
		initreportbuffer.append("\n");
		initreportbuffer.append("<a name=\"top\"></a>");
		initreportbuffer.append("\n");
		initreportbuffer.append("<table border='0' width='70%' height='47'>");
		initreportbuffer.append("\n");
		initreportbuffer.append("<tr>");
		initreportbuffer.append("\n");
		initreportbuffer.append("<td width='13%' bgcolor='#CCCCFF' align='center'><b><font color='#000000' face='Tahoma' size='2'>TestCase Name</font></b></td>");
		initreportbuffer.append("\n");
		initreportbuffer.append("<td width='18%' bgcolor='#CCCCFF' align='center'><b><font color='#000000' face='Tahoma' size='2'>Pass/Fail/Warning</font></b></td>");
		initreportbuffer.append("\n");
		initreportbuffer.append("<tr>");
		initreportbuffer.append("\n");
		

	}
	

   public void initTC(String testCaseName)throws IOException{
	   this.testCaseName= testCaseName ;
	    //Create current TC folder
	   this.Tc_FailNumber = 0;
	   this.Tc_PassNumber = 0 ;
       
	   String Tcstring="Detail of TestCase "+this.testCaseName;
       String initTcstring=inittcbuffer.toString();
      if(initTcstring.indexOf(Tcstring)==-1){
	   inittcbuffer.append("<br><br><br>\n");
	   inittcbuffer.append("<a name=\""+this.testCaseName+"\">\n");
	   inittcbuffer.append("<table border='1' width='70%' height='47'>\n");
	   inittcbuffer.append("<font size=\"4\"><b>Detail of TestCase "+this.testCaseName+" : \n");
	   inittcbuffer.append("<tr>\n");
	   inittcbuffer.append("<td width='40%' bgcolor='#CCCCFF'><b><font color='#000000' face='Tahoma' size='2'>Actions</font></b></td>\n");
	   inittcbuffer.append("<td width='60%' bgcolor='#CCCCFF' align='center'><b><font color='#000000' face='Tahoma' size='2'>Pass/Fail/Warning</font></b></td>\n");
	   inittcbuffer.append("</tr>\n");
      }   
	     
	   
	   

   }
   public void 	initAction(String action){
	   this.ActionName =action;
	   Pass_number = 0 ;
	   Fai_number= 0 ;
	   actionBuf = new StringBuffer();
	   actionBuf.append("<html>\n");
	   actionBuf.append("<title>Automation Test Results - Bleum</title>\n");
	   actionBuf.append("<head></head>\n");
	   actionBuf.append("<body>\n");
	   actionBuf.append("<font face='Tahoma'size='2'>\n");
	   actionBuf.append("<h2>Automation Test Results - Bleum</h2>\n");
	   actionBuf.append("<h5>"+this.testCaseName+": "+this.ActionName+"</h5>\n");
	   actionBuf.append("<h5><a href=\"../report.html\">Back to report home page</h5>\n");
	   actionBuf.append("<table border='1' bordercolor='#1E90FF' cellspacing='0.5' width='100%' height='47'>\n");
	   actionBuf.append("<tr>\n");
	   actionBuf.append("<td width='2%' bgcolor='#CCCCFF' align='center'><b><font color='#000000' face='Tahoma' size='2'>No</font></b></td>\n");
	   actionBuf.append("<td width='34%' bgcolor='#CCCCFF' align='center'><b><font color='#000000' face='Tahoma' size='2'>Expected Values</font></b></td>\n");
	   actionBuf.append("<td width='34%' bgcolor='#CCCCFF' align='center'><b><font color='#000000' face='Tahoma' size='2'>Actual Values</font></b></td>\n");
	   actionBuf.append("<td width='15%' bgcolor='#CCCCFF' align='center'><b><font color='#000000' face='Tahoma' size='2'>Result</font></b></td>\n");
	   actionBuf.append("<td width='16%' bgcolor='#CCCCFF' align='center'><b><font color='#000000' face='Tahoma' size='2'>screenshot</font></b></td>\n");
	   actionBuf.append("</tr>\n");
   }
   
   public void writeReport(String filedName ,String num, String expectedValue, String actualValue,
			String screenShot, boolean passed) {
	   Pass_number += (passed?1:0 );
	   Fai_number += (passed?0:1 );
	   actionBuf.append("<tr>\n");
	   actionBuf.append("<td bgcolor='#FFFFDC' valign='middle' align='center'>"+num+"</td>\n");
	   actionBuf.append("<td bgcolor='#FFFFDC' valign='middle' align='center'><b><font color='Green' face='Tahoma' size='2'>"+filedName+" : "+expectedValue+"</td>\n");
	   actionBuf.append("<td bgcolor='#FFFFDC' valign='middle' align='center'><b><font color='Green' face='Tahoma' size='2'>"+filedName+" : "+actualValue+"</font></b></td>\n");
	  
	   if (passed)
		   actionBuf.append("<td bgcolor='#FFFFDC' valign='middle' align='center'><b><font color='Green' face='Tahoma' size='2'>pass</font></b></td>\n");  
	   else
		   actionBuf.append("<td bgcolor='#FFFFDC' valign='middle' align='center'><b><font color='Red' face='Tahoma' size='2'>fail</font></b></td>\n");   
	  // actionBuf.append("<td bgcolor='#FFFFDC' valign='middle' align='center'><b><font color='Green' face='Tahoma' size='2'>"+(passed?"pass":"fail")+"</font></b></td>\n");
	   actionBuf.append("<td bgcolor='#FFFFDC' valign='middle' align='center'><b><font color='Green' face='Tahoma' size='2'><a href=\"./"+screenShot+"\">click</font></b></td>\n");
	   actionBuf.append("</tr>\n");
   }
   public void endAction(){
	   actionBuf.append("</body>\n");
	   actionBuf.append("</html>\n");
	   actionBuf.append("<hr><br>\n");
	   actionBuf.append("<table border='0' width='50%'>\n");
	   actionBuf.append("<tr><td width='100%' colspan='2' bgcolor='#000000'><b><font face='Tahoma' size='2' color='#FFFFFF'>Summary</font></b></td></tr>\n");
	   actionBuf.append("<tr><td width='45%' bgcolor='#E8FFE8'><b><font face='Tahoma' size='2'>Total Tests Passed</font></b></td><td width='55%' bgcolor='#E8FFE8'>"+this.Pass_number+"</td></tr>\n");
	   actionBuf.append("<tr><td width='45%' bgcolor='#FFE6FF'><b><font face='Tahoma' size='2'>Total Tests Failed</font></b></td><td width='55%' bgcolor='#FFE6FF'>"+Fai_number+"</td></tr>\n");
	   actionBuf.append("<tr><td width='45%' bgcolor='#FFFF99'><b><font face='Tahoma' size='2'>Total Tests Warned</font></b></td><td width='55%' bgcolor='#FFFF99'>0</td></tr>\n");
	   actionBuf.append("</table>\n");
		  try {
				Writer bw = new BufferedWriter(new FileWriter(folderPath +"\\" +testCaseName+"\\"+this.ActionName+".html"));
				bw.write(actionBuf.toString());
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	   
	   inittcbuffer.append("<tr>\n");
	   inittcbuffer.append("<td bgcolor='#FFFFDC' valign='top' align='justify'><a href=\"./"+this.testCaseName+"/"+this.ActionName+".html\">"+this.ActionName+"</td>\n");
	   inittcbuffer.append("<td bgcolor='#FFFFDC' valign='middle' align='center'><b><font color='Green' face='Tahoma' size='2'>"+this.Pass_number+"</font><font color='#000000'> / </font><font color='Red' face='Tahoma' size='2'>"+this.Fai_number+"<font color='#000000'> / </font></font></font><font color='Orange' face='Tahoma' size='2'>0</font></b></td>\n");
	   inittcbuffer.append("</tr>\n");
	  // inittcbuffer.append("</table>\n");
	   this.Tc_PassNumber+=this.Pass_number;
	   this.Tc_FailNumber+=this.Fai_number ;
	   this.Total_PassNumber+=this.Tc_PassNumber;
	   this.Total_FailNumber+=this.Tc_FailNumber ;
   }
   public void endTC(){
	   inittcbuffer.append("</table>\n");
	   initreportbuffer.append("<tr>\n");
	   initreportbuffer.append("<td bgcolor='#FDF5E6' valign='middle' align='center'><a  href='#"+this.testCaseName+"'>"+this.testCaseName+"</td>\n");
	   initreportbuffer.append("<td bgcolor='#FDF5E6' valign='middle' align='center'><b><font color='Green' face='Tahoma' size='3'>"+this.Tc_PassNumber+"</font><font color='#000000'> / </font><font color='Red' face='Tahoma' size='3'>"+this.Tc_FailNumber+"<font color='#000000'> / </font></font></font><font color='Orange' face='Tahoma' size='3'>0</font></b></td>\n");
	   initreportbuffer.append("<tr>\n");
   }
   public void endReport(){
	   System.out.println("endReport"+folderPath);
		initreportbuffer.append("</table>");
		initreportbuffer.append("</a>");
		initreportbuffer.append("<div style=\"margin-left:500px\"><BR><h8><a  href='#top'>Back to TOP</h5></div>");
	  try {
		Writer bw = new BufferedWriter(new FileWriter(this.folderPath+"\\report.html"));
		bw.write(initreportbuffer.toString());
		bw.write(this.inittcbuffer.toString());
		bw.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
   }
   public void emailReport(String emailSubject) throws Exception{
	   
	   String[] mailTo=this.mail_to;
	   String[] mailCc=this.mail_cc;
	   //String mailTo[]={"zym@china.zhaogang.com,long.yu@china.zhaogang.com"};  
       SendEmail sendemail=new SendEmail();	       
//       sendemail.send(emailSubject,mailTo,mailCc,this.startDate,this.getEndDate(),this.Total_PassNumber,this.Total_FailNumber,this.Total_Warning_number,this.folderPath);
	  
	    
   }

   @Test
   public void test() throws IOException {
	   String path = new File(".").getCanonicalPath()+"\\load.bat";
	   System.out.println(path);
   }
   

public String getActionName(){
	
	return this.ActionName;
	
}

public void setActionName(String actionName){
	
	this.ActionName=actionName;
	
}

public String getReportFilePath() {
	// TODO Auto-generated method stub
	return folderPath+"\\"+this.testCaseName;
}
   
   
   
   




}
